package geppetto.reordering;

import geppetto.cat.alignments.AlignmentsSet;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.SentenceInfo;
import geppetto.reordering.graph.ReorderingGraph;

import java.util.List;

public class PhraseMSDReorderingCalculator extends MSDReorderingCalculator{

	public PhraseMSDReorderingCalculator(AlignmentsSet set) {
		super(set);
	}

	@Override
	public ReorderingInfo getReorderingInfo(ExtractedPhrasePair epp) {
		ReorderingInfo info = new ReorderingInfo();
		SentenceInfo senInfo = epp.getSentenceInfo();
		if(senInfo.getCustomInfo("reordering-graph") == null){
			ReorderingGraph graph = getReorderingGraph(senInfo);
			senInfo.setCustomInfo("reordering-graph", graph);
		}
		
		ReorderingGraph graph = (ReorderingGraph)senInfo.getCustomInfo("reordering-graph");
		
		List<ExtractedPhrasePair> previousPhrases = graph.getPreviousNodes(epp);
		double monoAlphaP = 0;
		double swapAlphaP = 0;
		double otherAlphaP = 1;
		
		for(ExtractedPhrasePair pair : previousPhrases){
			int orientation = graph.getOrientation(pair, epp);
			if(orientation == MSDOrientationCodes.MONO_CODE){
				monoAlphaP = 1;
				otherAlphaP = 0;
				break;
			}
			if(orientation == MSDOrientationCodes.SWAP_CODE){
				swapAlphaP = 1;
				otherAlphaP = 0;
				break;
			}
		}
		if(previousPhrases.isEmpty()){
			monoAlphaP = 1;
			otherAlphaP = 0;
		}

		info.addPrevWordOrientation(MSDOrientationCodes.MONO_CODE, epp.getScore() * monoAlphaP);
		info.addPrevWordOrientation(MSDOrientationCodes.SWAP_CODE, epp.getScore() * swapAlphaP);
		info.addPrevWordOrientation(MSDOrientationCodes.OTHER_CODE, epp.getScore() * otherAlphaP);
		
		List<ExtractedPhrasePair> followingPhrases = graph.getFollowingNodes(epp);
		double monoAlphaN = 0;
		double swapAlphaN = 0;
		double otherAlphaN = 1;
		
		for(ExtractedPhrasePair pair : followingPhrases){
			int orientation = graph.getOrientation(epp, pair);
			double weight = graph.getPathsFromNode(pair);
			if(orientation == MSDOrientationCodes.MONO_CODE){
				monoAlphaN = 1;
				otherAlphaN = 0;
				break;
			}
			if(orientation == MSDOrientationCodes.SWAP_CODE){
				swapAlphaN = 1;
				otherAlphaN = 0;
				break;
			}
		}
		if(followingPhrases.isEmpty()){
			monoAlphaN = 1;
			otherAlphaN = 0;
		}
		
		info.addNextWordOrientation(MSDOrientationCodes.MONO_CODE,epp.getScore() * monoAlphaN);
		info.addNextWordOrientation(MSDOrientationCodes.SWAP_CODE,epp.getScore() * swapAlphaN);
		info.addNextWordOrientation(MSDOrientationCodes.OTHER_CODE,epp.getScore() * otherAlphaN);
	
		return info;
	}

	public static void main(String[] args) throws Exception{		
		ExtractedPhrasePair[] phrases = ReorderingGraph.getTestPhrases();
		int i = 0;
		PhraseMSDReorderingCalculator calc = new PhraseMSDReorderingCalculator(null);
		for(ExtractedPhrasePair pair : phrases){
			i++;
			ReorderingInfo info = calc.getReorderingInfo(pair);
			
			System.out.println(i + 
					" - " +
					info.getPreviousWordOrientationCounts().get(MSDOrientationCodes.MONO_CODE) + 
					" " +
					info.getPreviousWordOrientationCounts().get(MSDOrientationCodes.SWAP_CODE) + 
					" " +
					info.getPreviousWordOrientationCounts().get(MSDOrientationCodes.OTHER_CODE) + 
					" " +
					info.getNextWordOrientationCounts().get(MSDOrientationCodes.MONO_CODE) + 
					" " +
					info.getNextWordOrientationCounts().get(MSDOrientationCodes.SWAP_CODE) + 
					" " +
					info.getNextWordOrientationCounts().get(MSDOrientationCodes.OTHER_CODE));
		}

	}

	
	protected ReorderingGraph getReorderingGraph(SentenceInfo senInfo){
		ReorderingGraph graph = new ReorderingGraph(senInfo);		
		return graph;
	}

}

